<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>todoList</title>
    <style>
        input {
            outline: none;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 用户输入内容 -->
        <writing @gotobaba="receive"></writing>

        <!-- 渲染用户内容 -->
        <todo :ids="arr"></todo>

    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let writing = {
            template: `
            <input type="text" v-model="ipt" @keyup.enter="handlerIpt" autofocus>
            `,
            data() {
                return {
                    ipt: "",
                    list: [{
                        id: 0,
                        contents: "吃饭",
                        isChecked: false
                    }, {
                        id: 1,
                        contents: "水饺",
                        isChecked: true
                    }]
                }
            },
            created() {
                this.$emit("gotobaba", this.list);
            },
            methods: {
                handlerIpt() {
                    if (!this.ipt) return;
                    this.list.push({
                        id: this.list.length,
                        contents: this.ipt,
                        isChecked: false
                    });
                    this.ipt = "";
                    this.$emit("gotobaba", this.list);
                }
            }
        };

        let todo = {
            props: ["ids"],
            template: `
            <div>
                <h2>未完成的事情</h2>
                <div v-for="item in unfinishList">
                    <input type="checkbox" @click.prevent="handlerCheck(item)"> {{item.contents}}
                    <button @click="del(item)">删除</button>
                </div>

                <h2>已完成的事情</h2>
                <div v-for="item in finishList">
                    <input type="checkbox" checked @click.prevent="handlerCheck(item)"> {{item.contents}}
                    <button @click="del(item)">删除</button>
                </div>    
            </div>
            `,
            computed: {
                unfinishList() {
                    console.log(this.ids);
                    return this.ids.filter(ele => ele.isChecked === false)
                },
                finishList() {
                    return this.ids.filter(ele => ele.isChecked === true)
                }
            },
            methods: {
                handlerCheck(item) {
                    // this.ids[item.id].isChecked = !item.isChecked;
                    item.isChecked = !item.isChecked;
                },
                del(item) {
                    // this.ids.splice(item.id, 1);
                    this.ids.splice(this.ids.indexOf(item), 1);

                    //因为删除之后，id是不会动态的变化
                    //重新排序，给id动态赋值，为了后续删除
                    this.ids.forEach((ele, index) => {
                        ele.id = index
                    });
                }
            }
        };


        let vm = new Vue({
            el: "#app",
            components: {
                writing,
                todo
            },
            data: {
                arr: []
            },
            methods: {
                receive(data) {
                    this.arr = data;
                }
            }
        })
    </script>
</body>

</html>